package com.yyfax.ph.common.location;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.yyfax.common.CLog;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.home.LocalAreaManager;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定位服务类 获取用户的城市信息
 */
public class LocationManager implements AMapLocationListener {
	private static final String TAG = LocationManager.class.getSimpleName();

	public interface LocationListener {

		void onLocationSuccess(String city);

		void onLocationFailure();
	}

	private static volatile LocationManager mLocationManager;

	public static LocationManager getInstance(Context context) {
		if (mLocationManager == null) {
			synchronized (LocationManager.class) {
				if (mLocationManager == null) {
					CLog.i(TAG, "initialize LocationManager ...");
					mLocationManager = new LocationManager(context);
				}
			}
		}
		return mLocationManager;
	}

	//声明AMapLocationClient类对象
	private AMapLocationClient mLocationClient = null;
	private LocationListener mLocationListener;

	private LocationManager(Context ctx) {
		mLocationClient = new AMapLocationClient(ctx.getApplicationContext());
		mLocationClient.setLocationListener(this);
		//声明AMapLocationClientOption对象
		AMapLocationClientOption option = new AMapLocationClientOption();
		option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
		//获取一次定位结果：
		//该方法默认为false。
		option.setOnceLocation(true);
		//获取最近3s内精度最高的一次定位结果：
		//设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
		option.setOnceLocationLatest(true);
//		//设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
		option.setInterval(1000);
		//设置是否返回地址信息（默认返回地址信息）
		option.setNeedAddress(true);
		//设置是否强制刷新WIFI，默认为true，强制刷新。
		option.setWifiActiveScan(false);
		//设置是否允许模拟位置,默认为false，不允许模拟位置
		option.setMockEnable(false);
		//单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
		option.setHttpTimeOut(30000);
		//关闭缓存机制
		option.setLocationCacheEnable(false);
		//给定位客户端对象设置定位参数
		mLocationClient.setLocationOption(option);
	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		CLog.i(TAG, "获取定位信息返回");
		if (location != null) {
			if (location.getErrorCode() == 0) {
				CLog.i(TAG, "获取定位信息成功, ErrCode=" + location.getErrorCode());
				//可在其中解析amapLocation获取相应内容。
				parseLocationInfo(location);
			} else {
				//定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
				CLog.i(TAG, "获取定位信息失败, ErrCode=" + location.getErrorCode());
				CLog.i(TAG, "获取定位信息失败, errorInfo=" + location.getErrorInfo());
				YJPH.reportError("onLocationChanged(): ErrorCode=" + location.getErrorCode() + "; ErrorInfo=" + location.getErrorInfo());
				if (null != mLocationListener) {
					mLocationListener.onLocationFailure();
				}
			}
		}
	}

	private void parseLocationInfo(AMapLocation aMapLocation) {
//		aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
//		aMapLocation.getLatitude();//获取纬度
//		aMapLocation.getLongitude();//获取经度
//		aMapLocation.getAccuracy();//获取精度信息
//		aMapLocation.getAccuracy();//获取精度信息
//		aMapLocation.getCountry();//国家信息
//		aMapLocation.getProvince();//省信息
//		aMapLocation.getCity();//城市信息
//		aMapLocation.getDistrict();//城区信息
//		aMapLocation.getStreet();//街道信息
//		aMapLocation.getStreetNum();//街道门牌号信息
//		aMapLocation.getCityCode();//城市编码
//		aMapLocation.getAdCode();//地区编码
//		aMapLocation.getAoiName();//获取当前定位点的AOI信息
//		aMapLocation.getBuildingId();//获取当前室内定位的建筑物Id
//		aMapLocation.getFloor();//获取当前室内定位的楼层
//		aMapLocation.getGpsAccuracyStatus();//获取GPS的当前状态
		String province = aMapLocation.getProvince();
		String city = aMapLocation.getCity();
		if (null != LocalAreaManager.getInstance()) {
			LocalAreaManager.getInstance().setProvinceAndCityIndex(province, city);
		}
		//获取定位时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(aMapLocation.getTime());
		df.format(date);
		if (null != mLocationListener) {
			mLocationListener.onLocationSuccess(city);
		}
		CLog.i(TAG, "获取到的定位城市信息是" + province + "获取到的定位省信息是" + city);
		CLog.i(TAG, "获取到的定位城市信息是" + city);
		CLog.i(TAG, "获取到的定位省信息是" + province);

	}


	public void setLocationListener(LocationListener locationListener) {
		mLocationListener = locationListener;
	}

	public void startLocation(LocationListener locationListener) {
		mLocationListener = locationListener;
		if (null != mLocationClient) {
			mLocationClient.startLocation();
			CLog.i(TAG, "开始获取定位信息");
		}
	}

	public void stopLocation() {
		if (null != mLocationClient) {
			mLocationClient.stopLocation();//停止定位后，本地定位服务并不会被销毁
		}
	}

	public void releaseLocation() {
		if (null != mLocationClient) {
			mLocationClient.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
		}
	}

	public static void destroy() {
		if (null != mLocationManager) {
			mLocationManager.releaseLocation();
		}
	}
}
